import logging

from fastapi import FastAPI
from starlette.staticfiles import StaticFiles

from app.providers import app_provider
from app.providers import logging_provider
from app.providers import handle_exception
from app.providers import route_provider


def create_app() -> FastAPI:
    logging.info("App initializing")

    app = FastAPI(
        title='Automate API Docs',
        description='Automate教程 AutomateAPI接口文档，项目代码：https://gitee.com/season-fan',
        version='1.0.0',
        docs_url='/docs',    #简单版接口地址
        redoc_url='/redocs', #详细版接口地址
    )
    """
    挂载静态路由目录
    """
    app.mount(path='/static', app=StaticFiles(directory='./static'), name='static')
    
    """
    注册全局组件
    """
    register(app, app_provider)
    register(app, logging_provider)
    register(app, handle_exception)

    boot(app, route_provider)

    return app


def register(app, provider):
    logging.info(provider.__name__ + ' registering')
    provider.register(app)


def boot(app, provider):
    logging.info(provider.__name__ + ' booting')
    provider.boot(app)
